<%@page import="com.framework.entity.Global"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!doctype html>
<html class="no-js">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>添加角色</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <meta name="apple-mobile-web-app-title" content="Amaze UI" />
    <jsp:include page="../inc.jsp"></jsp:include>
</head>

<body>
<!-- content start -->
<div class="admin-content rightcont">

    <div class="am-cf am-padding">
        <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">添加角色</strong></div>
    </div>
    <div class="am-g" style="padding:15px 0;background:#fff;border-top:1px solid #ccc;border-bottom:1px solid #ccc;"  data-am-sticky="{top:51,animation: 'slide-top'}">
        <div class="am-u-sm-12 am-u-md-12" >
            <div>
                <form id="form-search" onsubmit="return false;">

                    <span style="font-size:14px">名称</span>
                    <input style="font-size : 14px; margin-right:20px;" type="text" name="text" id="form_text">
                    <!-- <span style="font-size:14px">平台</span> -->
                    <input type="hidden" id="platform_list_text" name="platform">
                    <select  id="platform_list_select"  onchange="queryPlatform();">
                        <option value="" selected="selected">所有平台</option>
                        <option value="0">管理后台</option>
                        <option value="5">商城后台</option>
                    </select>
                    <button class="am-btn am-btn-primary am-btn-xs"  style="margin-right:5px; margin-left: 20px;"onclick="query();"><i class="fa fa-search"></i>查询</button>
                    <button class="am-btn am-btn-primary am-btn-xs" onclick="queryAll();"><i class="fa fa-search"></i>清空</button>

                </form>
            </div>
        </div>
    </div>
    <div class="am-g" style="padding:15px 0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;">
        <div class="am-u-sm-12 am-u-md-6">
            <div class="am-btn-toolbar">
                <div class="am-btn-group"><button type="button" onclick="add()" class="am-btn am-btn-xs am-btn-primary am-radius "><span class="am-icon-plus"></span>添加</button></div>
                <div class="am-btn-group"><button type="button" onclick="removeAll()" class="am-btn am-btn-xs am-btn-primary am-radius"><span class="am-icon-remove"></span>删除</button></div>
            </div>


        </div>
    </div>

    <div class="am-u-sm-12">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12 tablemenu">
            <div id="dtGridContainer_2_1_9" class="dt-grid-container"></div>
            <div id="dtGridToolBarContainer_2_1_9" class="dt-grid-toolbar-container"></div>
        </div>
    </div>
</div>
<!-- content end -->
<!--新增弹出 start-->
<div class="am-modal am-modal-no-btn" tabindex="-1" id="doc-modal-1" style="width: 600px;top:52%;margin-left:-300px;">
    <div class="admin-content" style="height:300px">
        <div class="am-cf am-padding">
            <div class="am-fl am-cf"><strong id="operation" class="am-text-primary am-text-lg">新增角色</strong></div>
        </div>
        <form id="form" name="form" >
            <input type="hidden" id="form_type">
            <div class="am-tabs am-margin">
                <div class="am-u-sm-12 djzjsxtc">
                    <div class="am-u-sm-4 am-text-center">
                        角色名称：
                    </div>
                    <div class="am-u-sm-8">
                        <input type="hidden" name="prole.id" id="proleId">
                        <input type="text" maxlength="50"  id = "text" name = "prole.text">
                    </div>
                </div>
                <div class="clear"></div>
            </div>
        </form>
        <div class="am-margin am-text-right">
            <button type="button" class="am-btn am-btn-primary am-btn-xs"  onclick = "save();">提交保存</button>
            <button type="button" class="am-btn am-btn-primary am-btn-xs" data-am-modal-close>放弃保存</button>
        </div>

    </div>
    <div id="foam-zcc"></div>
</div>
<!--增加属性弹出 end-->
<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">提示</div>
        <div class="am-modal-bd">
            <span id="msg_alert">Hello world！</span>
        </div>
        <div class="am-modal-footer">
            <span class="am-modal-btn">确定</span>
        </div>
    </div>
</div>
<!-- 删除提示框 -->
<div class="am-modal am-modal-confirm" tabindex="-1" id="my-confirm">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">提示</div>
        <div class="am-modal-bd">
            <span id="msg_alertDEL">Hello world！</span>
        </div>
        <div class="am-modal-footer">
            <input type="hidden" id="del_id">
            <input type="hidden" id="del_platform">
            <span class="am-modal-btn" data-am-modal-cancel>取消</span>
            <span class="am-modal-btn" data-am-modal-confirm onclick="doDel()">确定</span>
        </div>
    </div>
</div>

</body>
<script src="${pageContext.request.contextPath}/resources/js/jquery/ajaxfileupload.js" type="text/javascript"></script>

<script>

    //表格初始化
    var dtGridOption_2_1_9 = {
        lang : 'zh-cn',
        ajaxLoad : true,
// 		loadAll : true,  //如果是分页数据 ,此项不用写
        exportFileName : '消息目录',
        check:true,
        loadURL : '${pageContext.request.contextPath}/user/showRoleGrid.action',//只支持Post模式，返回值必须是一套符合JSON数组的文本
        columns : [
            {
                id : 'id',
                title : 'id',
                type:'string',
                hide:true,
                columnClass:'text-center'
            },
            {
                id : 'platform',
                title : '平台',
                type:'string',
                columnClass:'text-center',
                resolution : function(value, record, column, grid, dataNo, columnNo){
                    //value：当前单元格的值  record：记录集，可以直接获取该行的数据 column：列对象 grid：表格对象  dataNo：数据编号，从0开始  columnNo：列编号，从0开始
                    if (value==0){
                        return "<font>管理后台</font>";
                    } else if(value==1){
                        return "<font>统一调度平台</font>";
                    }else if(value==2){
                        return "<font>花店</font>";
                    }else if(value==3){
                        return "<font>经销商</font>";
                    }else if(value==5){
                        return "<font>商城后台</font>";
                    }
                }
            },
            {
                id : 'text',
                title : '名称',
                type:'string',
                columnClass:'text-center'
            },
            {
                id:'role',
                title:'角色',
                type:'string',
                columnClass:'text-center'
            },
            {
                id : 'createTime',
                title : '创建时间',
                type:'string',
                columnStyle:'width:80px',
                columnClass:'text-center'
            },
            {
                id:'id',
                title : '操作',
                columnClass:'text-center',
                columnStyle:'width:300px',
                resolution : function(value, record, column, grid, dataNo, columnNo){
                    //value：当前单元格的值  record：记录集，可以直接获取该行的数据 column：列对象 grid：表格对象  dataNo：数据编号，从0开始  columnNo：列编号，从0开始
                    var content ='<button class="btn btn-xs btn-default"  onclick="edit(\''+value+'\',\''+record.platform+'\');"><i class="fa fa-edit"></i>  编辑</button>';
                    content += '  ';
                    content += '  ';
                    content+='<button class="btn btn-xs btn-danger" onclick="del(\''+value+'\',\''+record.platform+'\');"><i class="fa fa-trash-o"></i>  删除</button>';
                    return 	content;
                }
            }
        ],
        gridContainer : 'dtGridContainer_2_1_9',
        toolbarContainer :'dtGridToolBarContainer_2_1_9',
        tools : 'refresh',//|faseQuery|advanceQuery|export[excel,csv,pdf,txt]|print
        pageSize : 10,
        pageSizeLimit : [10, 20, 50]

    };

    var grid_2_1_9 = $.fn.DtGrid.init(dtGridOption_2_1_9);


    $(function(){
        //loadPlatformList();
        grid_2_1_9.load();
        grid_2_1_9.reload();
        //treeinit();
    });
    //保存
    function treeinit(data)
    {$('#role_append').jstree({
        'plugins': ["wholerow", "checkbox", "types"],
        'ui' : {
            'select_parent_open':'true',
            'selected_parent_close' : 'select_parent',
            'initially_select' : [ "1000" ]
        },
        'core': {
            'themes': {
                'responsive': false,
                'icons' : true,
                'dots	' : true
            },
            check_callback : true,
            'data': data,
            'types': {
                'default': {
                    draggable : false //设置节点不可拖拽
                },
                'file': {
                }
            }
        },
        callback : {
            onselect : function(node,tree_obj){
                alert(node);
            }
        },

    });}
    function save(){
        var flag = true;
        var msg = "操作失败";
        if(flag)if($("#text").val() == ""||$("#text").val() ==null){flag = false;msg = "请填写名称！";}
        if(flag)if($("#platform").val() == ""||$("#platform").val() ==null){flag = false;msg = "请选择平台！";}
        if(flag){
            var type=$("#form_type").val();
            if(type==0){
                $.ajax({
                    url:'${pageContext.request.contextPath}/role/addRole.action',
                    data:$('#form').serialize(),
                    type: 'post',
                    async : false,
                    success:function(data){
                        $("#msg_alert").html("新增成功");
                        $('#my-alert').modal('open');
                        query();
                        $("#doc-modal-1").modal('close');
                    }
                });
            }
            else if(type==1){
                $.ajax({
                    url:'${pageContext.request.contextPath}/role/editRole.action',
                    data:$('#form').serialize(),
                    type: 'post',
                    async : false,
                    success:function(data){
                        $("#msg_alert").html("修改成功");
                        $('#my-alert').modal('open');
                        query();
                        $("#doc-modal-1").modal('close');
                    }
                });
            }
        }else{
            $("#msg_alert").html(msg);
            $('#my-alert').modal('open');
        }
    }
    function saveprvi()
    {
        var data=$('#role_append').jstree().get_selected(true);
        var ids=[];
        for(var i=0;i<data.length;i++)
        {
            if(!contains(data[i].id,ids))
            {ids.push(data[i].id);}
            ids= getParent(data[i],ids);//获取所有父节点
        }
        $("#privIds").val(ids);
        //alert(ids);
        $.ajax({
            url:'${pageContext.request.contextPath}/role/savePriv.action',
            data:$('#formpriv').serialize(),
            type: 'post',
            async : false,
            success:function(data){
                $("#msg_alert").html("修改成功");
                $('#my-alert').modal('open');
                query();
                $("#doc-modal-2").modal('close');
                // $("#foam-zcc").hide();
            }
        });
    }
    function getParent(child,ids)
    {

        if(child.parent!="#")
        {
            var par=$('#role_append').jstree().get_node(child.parent);
            ids.push(par.id);
            getParent(par,ids);
        }
        return ids;
    }
    //用于比较字符串数组里面有没有这个字符串
    function contains(a, obj) {
        var i = a.length;

        while (i--) {
            if (a[i] == obj) {
                return true;
            }
        }
        return false;
    }

    //删除
    function del(id,platform){
        $("#del_id").val(id);
        $("#del_platform").val(platform);
        $("#msg_alertDEL").html("确定要删除吗?");
        $("#my-confirm").modal('open');
    }
    function doDel(){
        var id=$("#del_id").val();
        var platform=$("#del_platform").val();
        $.ajax({
            url:'${pageContext.request.contextPath}/role/deleteRole.action?ids='+id+'&platform='+platform,
            type: 'post',
            async : false,
            success:function(data){
                query();
            }
        });
    }

    //批量删除
    function removeAll(){
        var platform=$("#platform_list_select").val();
        if(platform=="")
        {
            $("#msg_alert").html("批量删除的时候请选择所在平台,只可以批量删除同一平台的");
            $('#my-alert').modal('open');
        }else
        { var ids=[];
            var recordCount = grid_2_1_9.getCheckedRecords();
            for(var i=0;i<grid_2_1_9.getCheckedRecords().length;i++){
                ids.push(recordCount[i]["id"]);
            }
            $("#msg_alertDEL").html("确定要删除吗?");
            $("#my-confirm").modal('open');
            $("#del_id").val(ids);
            $("#del_platform").val(platform);}
    }

    function add(){
        $("#operation").html("新增角色");
        $("#form_type").val("0");
        $(':input','#form').val('');
        $("#platform").val(0);
        $("#doc-modal-1").modal('open');
        /* $("#selectRec").show(); */
//self.location="${pageContext.request.contextPath}/jsp/base/message/message_addOrEdit.jsp";
    }


    //跳转编辑
    function edit(id,platform){
        $("#operation").html("编辑角色");
        $("#form_type").val("1");
        $("#proleId").val(id);
        $.ajax({
            url:'${pageContext.request.contextPath}/role/toEdit.action?id='+id+'&platform='+platform,
            type: 'post',
            async : false,
            success:function(data){
                for(var s in eval(data)[0]){
                    $("#"+s).val(eval(data)[0][s]);
                }
                $("#platform").val(eval(data)[0]['platform']);
                $("#doc-modal-1").modal('open');
            }
        });
    }
    function queryPlatform()
    {
        $("#platform_list_text").val($("#platform_list_select").val());
        query();
    }
    //条件查询
    function query()
    {	//传入实例化好的dt-grid 如grid_2_1_9和条件筛选的form的id
        //如果有其他固定的筛选条件，请在条件筛选的form中放入隐藏域

        dtGridLoad(grid_2_1_9, "form-search");
    }

    //清空条件查询
    function queryAll()
    {	//传入实例化好的dt-grid 如grid_2_1_9和条件筛选的form的id
        //如果有其他固定的筛选条件，请在条件筛选的form中放入隐藏域
        $("#form_text").val("");
        $("#platform_list_text").val("");
        $("#platform_list_select").val("");
        dtGridLoad(grid_2_1_9, "form-search");
    }
    //加载平台列表
    function loadPlatformList() {
        $
                .ajax({
                    url : '${pageContext.request.contextPath}/platform/loadList.action',
                    type : 'post',
                    async : false, //同步
                    success : function(data) {
                        platformList = data;
                        for ( var i = 0; i < data.length; i++) {

                            $("#platform_list_select").append(
                                    "<option value='"+data[i].id+"'>"
                                    + data[i].name + "</option>");
                            $("#platform").append(
                                    "<option value='"+data[i].id+"'>"
                                    + data[i].name + "</option>");
                        }
                    }
                });
    }

    function bjtcc(id,platform) {
        roleId=id;
        var value;
        $.ajax({
            url : '${pageContext.request.contextPath}/role/getMenuByRoleId.action?id='
            + id+'&platform='+platform,
            type : 'post',
            async : false, //同步
            success : function(data) {
                //alert(JSON.stringify(data));
                $('#role_append').jstree("destroy");
                treeinit(data);
                //$('#role_append').jstree("refresh");
                //$('#role_append').jstree("load_all("+JSON.stringify(data)+",loadOk())");
                //alert(12);

                $("#privPlatform").val(platform);
                $("#doc-modal-2").modal('open');
                $("#privId").val(id);
            }

        });
    }
    function loadOk()
    {//alert(1);
    }
</script>
</html>
